package com.youhong.teethcare.customviews;

import android.content.ClipData;
import android.content.ClipDescription;
import android.content.Context;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatImageView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.DragEvent;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

/**
 * Created by Administrator on 2018/5/16 0016.
 */

public class ImageViewSwitchBar extends AppCompatImageView implements GestureDetector.OnGestureListener {

    public ImageViewSwitchBar(Context context) {
        super(context);
    }

    public ImageViewSwitchBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public ImageViewSwitchBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    GestureDetector mGestureDetector;

    @Override
    public boolean onDragEvent(DragEvent event) {
        return super.onDragEvent(event);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mGestureDetector = new GestureDetector(getContext(), this);
    }

    private float previous_x = 0;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        //boolean retVal = mGestureDetector.onTouchEvent(event);
        if (event.getAction() == MotionEvent.ACTION_UP) {
            isDown = false;

            return true;
        } else if (event.getAction() == MotionEvent.ACTION_DOWN) {

            isDown = true;
            previous_x = event.getX();
            return true;
        } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
            // Log.e("abc", "X=" + event.getX() + ",Y=" + event.getY());
            if (isDown) {
                //这里的MoveX 是相对ACTION_DOWN 时的X 偏移。
                float moveX = (event.getX() - previous_x);//获取偏移量
               // Log.e("abc", "distance:" + moveX);
                LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) this.getLayoutParams();

                //触发左边界
                if (lp.leftMargin + moveX < minLeftMargin) {
                    lp.leftMargin = minLeftMargin;


                    //触发右边界
                } else if (lp.leftMargin + moveX > maxLeftMargin) {
                    lp.leftMargin = maxLeftMargin;

                    //当没有触发边界的时候
                } else {
                    lp.leftMargin += moveX;//设置LeftMargin 来移动Thumb
                }
                //返回LeftMargin 给Activity 做判断
                if (onThumbMoveListener != null) {
                    onThumbMoveListener.onThumbMoved(lp.leftMargin);
                }
                this.setLayoutParams(lp);
                //previous_x += moveX;
                return true;
            }
        }


        return false;
        // return retVal || super.onTouchEvent(event);
    }

    public void setMinLeftMargin(int minLeftMargin) {
        this.minLeftMargin = minLeftMargin;
    }

    public void setMaxLeftMargin(int maxLeftMargin) {
        this.maxLeftMargin = maxLeftMargin;
    }

    int minLeftMargin = 0;
    int maxLeftMargin = 0;

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        getParent().requestDisallowInterceptTouchEvent(true);
        return super.dispatchTouchEvent(event);
    }

    boolean isDown = false;

    @Override
    public boolean onDown(MotionEvent e) {

        return true;
    }

    @Override
    public void onShowPress(MotionEvent e) {

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {

        return true;
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {


        return false;
    }

    @Override
    public void onLongPress(MotionEvent e) {

    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        return false;
    }

    public void setOnThumbMoveListener(OnThumbMoveListener onThumbMoveListener) {
        this.onThumbMoveListener = onThumbMoveListener;
    }

    OnThumbMoveListener onThumbMoveListener;

    public interface OnThumbMoveListener {

        void onThumbMoved(int leftMargin);
    }

}
